package com.cashmama.market.api.domain.handler;

import com.alibaba.fastjson.JSONObject;
import com.cashmama.market.api.domain.vo.RequestLocalInfo;
import com.cashmama.market.api.domain.vo.order.PermissionTipVo;
import com.cashmama.market.api.domain.vo.order.PermissionsInfo;
import com.cashmama.market.api.infras.config.ApolloConfigUtil;
import com.fqgj.common.api.Response;
import com.fqgj.xjd.user.client.UserAuthSmsService;
import com.fqgj.xjd.user.client.enums.UserAuthSmsTypeEnum;
import com.fqgj.xjd.user.client.response.UserAuthSmsInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: market-api
 * @description
 * @author: frank
 * @create: 2020-01-21 11:16
 **/
@Service
public class ConfigHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(ConfigHandler.class);

    @Autowired
    private ApolloConfigUtil apolloConfigUtil;
    @Autowired
    private UserAuthSmsService userAuthSmsService;

    public boolean needAlgo360Verify(String userCode) {
        if (!apolloConfigUtil.orderALGO360Verify()) {
            return false;
        }
        Response<UserAuthSmsInfo> userAuthSmsInfo = userAuthSmsService.getUserAuthSmsInfo(userCode, UserAuthSmsTypeEnum.ALGO_360);
        if (userAuthSmsInfo.getData() != null) {
            return false;
        }
        return true;
    }

    public PermissionsInfo getPermissionsInfoFromString(String merchantName) {
        LOGGER.info("== 获取下单权限 ==");
        if (!apolloConfigUtil.getPermissionsSwitch()) {
            LOGGER.info("== 获取下单权限 开关关闭==");
            return null;
        }
        try {

            PermissionsInfo permissionsInfo = JSONObject.parseObject(apolloConfigUtil.getPermissions(), PermissionsInfo.class);
            LOGGER.info("== 获取下单权限 配置内容==,data:{}", apolloConfigUtil.getPermissions());
            //提交订单时候的权限提示
            if(merchantName.equals("")){
                permissionsInfo.setTips("To assess your eligibility and facilitate faster disbursal of your loan,we need permissions to access you Phone,Storage,Camera and Contact");
            }else {
                permissionsInfo.setTips("");
            }
            List<PermissionTipVo> permissionTipVos = new ArrayList<>();
//            if (RequestLocalInfo.getRequestBasicInfo().getVersionCode() > apolloConfigUtil.getDefaultOnlineVersionCode()){
//                permissionTipVos.add(new PermissionTipVo("SMS", "We collect and monitor your non personal messages related to financial transactions, name of sender, description and amount of transaction for the purpose of performing credit risk assessment. No personal SMS data is collected,read or stored"));
//            }
//            permissionTipVos.add(new PermissionTipVo("Contacts", "During your loan application, we collect all of your phonebook contacts which includes their phone contact names,phone numbers,contact added date to enrich your credit profile and to allow you to choose your references in the loan application , to reach you in case of any default , and to share our app invites with your contacts."));
//            permissionTipVos.add(new PermissionTipVo("Phone", "Our app collects and monitors specific information about your device including your hardware model,OS version,IP address,unique device identifiers such as IMEI, SIM information to uniquely identify the device and ensure no unauthorised device acts on your behalf to prevent fraud. this also helps us to improve your credit assessment."));
//            permissionTipVos.add(new PermissionTipVo("Installed Apps","We collect each installed apps metadata information such as app name,package name,installed time, update time on your device to improve your credit assessment and to create personalised offers."));
            permissionTipVos.add(new PermissionTipVo("Contacts List", "To meet the various needs of partners. Cashmall will upload and transmit your contact list data including name and phone number from your phone to https://dc2-api.rupee-mama.in. the uploaded and transmitted name and phone number will be used to manage risks and conduct anti-fraud checks. while using this App, it sends contact list data to our server when you submit your application. Cashmall will never share data with others without your permission. the collection will only be done if you agree to the authorization"));
            permissionTipVos.add(new PermissionTipVo("Phone Info", "To meet the various needs of partners.Cashmall will collects and monitors specific information about your device including your hardware model,OS version,IP address,unique device identifiers such as IMEI, SIM information to uniquely identify the device and ensure no unauthorised device acts on your behalf to prevent fraud. this also helps us to improve your credit assessment."));
            permissionTipVos.add(new PermissionTipVo("Installed Apps", "To meet the various needs of partners. Cashmall will collect each installed apps metadata information such as app name,package name,installed time, update time on your device to improve your credit assessment and to create personalised offers."));
            if (StringUtils.isEmpty(merchantName)) {
                permissionTipVos.add(new PermissionTipVo("Access camera", "To meet the various needs of partners. We need to get the camera permission of your mobile phone, because we need you to submit a current selfie when you applying for a loan from our partner, so as to ensure that the loan is initiated by yourself and prevent loan disputes"));
            }
            permissionsInfo.setPermissionList(permissionTipVos);
            return permissionsInfo;
        } catch (Exception e) {
            LOGGER.error("== 获取下单权限失败 ==", e);
        }
        return null;
    }

    public PermissionsInfo getPermissionsInfoOfSmsFromString() {
        try {
            PermissionsInfo permissionsInfo = JSONObject.parseObject(apolloConfigUtil.getPermissionsOfSms(), PermissionsInfo.class);
            return permissionsInfo;
        } catch (Exception e) {

        }
        return null;
    }
}
